package com.jinpeng.rabbitmq.topic;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author JINPENG
 * @Todo  主题交换机配置
 */
@Configuration
public class TopicRabbitConfig {
    /**
     * 绑定键
     */
    public final static String CAR = "topic.car";
    public final static String HOUSE = "topic.house";
    public final static String ALL = "topic.all";

    /**
     * 注册用户专门传递man的消息队列
     * @return  Queue
     */
    @Bean
    public Queue carQueue() {
        return new Queue(TopicRabbitConfig.CAR);
    }

    /**
     * 注册用户专门传递woman的消息队列
     * @return  Queue
     */
    @Bean
    public Queue houseQueue() {
        return new Queue(TopicRabbitConfig.HOUSE);
    }
    /**
     * 注册携带topic的消息
     * @return  Queue
     */
    @Bean
    public Queue allQueue() {
        return new Queue(TopicRabbitConfig.ALL);
    }

    /**
     * 注册名为topicExchange 的topic交换机
      * @return  TopicExchange
     */
    @Bean
    TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }



    /**
     * 将队列与交换机绑定,并绑定键值为topic.man,只要是消息携带的路由键是topic.man,会分发到该队列
     * @return
     */
    @Bean
    Binding bindingCarMessage() {
        return BindingBuilder.bind(carQueue()).to(exchange()).with(CAR);
    }

    /**
     * 将队列与交换机绑定,并绑定键值为topic.woman,只要是消息携带的路由键是topic.woman,会分发到该队列
     * @return
     */
    @Bean
    Binding bindingHouseMessage2() {
        return BindingBuilder.bind(houseQueue()).to(exchange()).with(HOUSE);
    }

    /**
     * 将队列与交换机绑定,并绑定键值为topic.#,只要是消息携带的路由键是topic,会分发到该队列
     * @return
     */
    @Bean
    Binding bindingExchangeMessage3() {
        return BindingBuilder.bind(allQueue()).to(exchange()).with("topic.#");
    }
}
